<template>
	<view class="content">
		<view class="searchBox">
			<u-search placeholder="请输入仓库名称或负责人" v-model="keyword" @search="searchFun" @clear="clearFun"
				:show-action="true" action-text="搜索" bg-color="#F8F8F8"></u-search>
		</view>
		
		<z-paging ref="paging" v-model="dataList" @query="queryList" :fixed="false"
			:auto-show-back-to-top="true" :auto-clean-list-when-reload="false"
			:auto-scroll-to-top-when-reload="false" :show-loading="true"
			:default-page-size="10" :use-cache="false">
			<view slot="top">
				<view class="operateBox">
					<view class="btnBox">
						<u-button type="primary" size="mini" @click="pageAddFun">新增仓库</u-button>
					</view>
				</view>
			</view>
			
			<view class="listBox">
				<view v-for="(item, index) in dataList" :key="index" class="listItem" 
					@click="selectDetailFun(item, index)">
					<view class="itemHeader">
						<view class="title">{{item.name || '未命名仓库'}}</view>
						<view class="status" :class="{'status-enable': item.visible === 1, 'status-disable': item.visible === 0}">
							{{item.visible === 1 ? '启用' : '禁用'}}
						</view>
					</view>
					<view class="itemContent">
						<view class="infoLine">
							<text class="label">负责人：</text>
							<text class="value">{{item.manage_user_names || '暂无'}}</text>
						</view>
						<view class="infoLine">
							<text class="label">创建时间：</text>
							<text class="value">{{item.create_time || '暂无'}}</text>
						</view>
					</view>
				</view>
			</view>
		</z-paging>
	</view>
</template>

<script>
	let that = '';
	import { apiStockStore } from '@/api/crm.js'
	export default {
		data() {
			return {
				keyword: '',
				dataList: [],
				queryParams: {
					pageNum: 1,
					pageSize: 10
				}
			}
		},
		onLoad(e) {
			that = this;
			// 判断是否是从出库单页面跳转过来的
			if (e.type === 'add_stockout_goback') {
				that.pageType = 'add_stockout_goback';
			}
		},
		methods: {
			// 查询列表
			queryList(pageNo, pageSize) {
				that.queryParams.pageNum = pageNo;
				that.queryParams.pageSize = pageSize;
				
				let reqData = {
					action: 'get_list',
					params: {
						matchObj: {},
						sortObj: {
							create_time: 'desc'
						},
						pageNum: that.queryParams.pageNum,
						pageSize: that.queryParams.pageSize
					}
				}
				
				// 添加搜索条件
				if (that.keyword) {
					reqData.params.matchObj.name = { $like: `%${that.keyword}%` };
				}
				
				apiStockStore(reqData)
					.then(res => {
						console.log('接口返回数据:', res);
						if (res.code === 1) {
							// 正确处理成功返回的数据
							console.log('数据列表:', res.data.data);
							console.log('数据列表长度:', res.data.data.length);
							// 直接将数据赋值给dataList
							that.dataList = res.data.data || [];
							that.$refs.paging.complete(res.data.data || []);
						} else {
							console.log('接口返回错误:', res.msg);
							that.$refs.paging.complete(false);
						}
					})
					.catch(err => {
						console.log('接口请求错误:', err);
						that.$refs.paging.complete(false);
					})
			},
			// 搜索
			searchFun() {
				that.$refs.paging.reload();
			},
			// 清空搜索
			clearFun() {
				that.keyword = '';
				that.$refs.paging.reload();
			},

			// 新增仓库
			pageAddFun() {
				uni.navigateTo({
					url: './add'
				})
			},
			// 查看详情
			selectDetailFun(item, index) {
				// 判断是否是从出库单页面跳转过来的选择模式
				if (that.pageType === 'add_stockout_goback') {
					// 触发全局事件，将选择的仓库信息回传给出库单页面
					uni.$emit('stockStoreBindFun', {
						store_id: item.id,
						store_name: item.name
					});
					// 返回上一页
					uni.navigateBack();
				} else {
					// 正常查看详情模式
					uni.$stockStoreInfo = item;
					uni.navigateTo({
						url: './detail?index=' + index
					})
				}
			}
		}
	}
</script>

<style>
	page {
		height: 100%;
	}
	
	.content {
		height: 100%;
		background-color: #F5F5F5;
	}
	
	.searchBox {
		padding: 20rpx;
		background-color: #FFFFFF;
	}
	
	.operateBox {
		padding: 20rpx;
		background-color: #FFFFFF;
		border-bottom: 1rpx solid #F0F0F0;
	}
	
	.btnBox {
		display: flex;
		justify-content: flex-end;
	}
	
	.listBox {
		padding: 20rpx;
	}
	
	.listItem {
		background-color: #FFFFFF;
		border-radius: 10rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);
	}
	
	.itemHeader {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
	}
	
	.title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
	}
	
	.status {
		font-size: 24rpx;
		padding: 4rpx 12rpx;
		border-radius: 20rpx;
	}
	
	.status-enable {
		background-color: #F0F9FF;
		color: #409EFF;
	}
	
	.status-disable {
		background-color: #FFF6F6;
		color: #F56C6C;
	}
	
	.itemContent {
		margin-bottom: 20rpx;
	}
	
	.infoLine {
		display: flex;
		margin-bottom: 8rpx;
		font-size: 28rpx;
	}
	
	.label {
		width: 120rpx;
		color: #666;
	}
	
	.value {
		flex: 1;
		color: #333;
	}
	
	.itemFooter {
		border-top: 1rpx solid #F0F0F0;
		padding-top: 16rpx;
	}
	
	.time {
		font-size: 24rpx;
		color: #999;
	}
</style>